package com.aijk.ehm.api;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.exception.ErrorCodeException;
import com.aijk.commons.lang.utils.StringUtil;
import com.aijk.ehm.model.*;
import com.aijk.ehm.model.qo.SemBpQo;
import com.aijk.ehm.model.qo.SemRecordQo;
import com.aijk.ehm.model.qo.SemStepQo;
import com.aijk.ehm.service.BasUserService;
import com.aijk.ehm.service.EhmResidentService;
import com.aijk.ehm.service.SemRecordService;
import com.aijk.ehm.service.SemStepService;
import com.aijk.ehm.utils.SemConst;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * 测量记录
 */
@Service("semRecordApiImpl")
@Transactional
public class SemRecordApiImpl implements SemRecordApi {
    private static Logger logger = LoggerFactory.getLogger(SemRecordApiImpl.class);

    @Autowired
    private SemRecordService service;

    @Autowired
    private EhmResidentService ehmResidentService;

    @Autowired
    private BasUserService basUserService;

    @Autowired
    private SemBpApi semBpService;

    @Autowired
    private SemHeightApi semHeightService;

    @Autowired
    private SemWeightApi semWeightService;

    @Autowired
    private SemStepApi semStepService;

    @Autowired
    private SemStepService semStepServices;

    @Override
    public Long save(SemRecord bean) throws ErrorCodeException {
        service.save(bean);
        return bean.getId();
    }

    @Override
    public void update(SemRecord bean) throws ErrorCodeException {
        service.update(bean);
    }

    @Override
    public SemRecord findOne(Long id) throws ErrorCodeException {
        return service.findOne(id);
    }

    @Override
    public List<SemRecord> list(SemRecordQo qo) {
        return service.find(qo);
    }

    @Override
    public void deleteById(Long id) throws ErrorCodeException {
        service.delete(id);
    }

    @Override
    public int count(SemRecordQo qo) {
        return service.count(qo);
    }

    @Override
    public PageInfo<SemRecord> page(SemRecordQo qo) {
        PagingRequest pagingRequest = new PagingRequest(qo.getPageNo().intValue(), qo.getPageSize().intValue());
        return service.page(pagingRequest,qo);
    }

    @Override
    @Transactional
    public Long saveHm(HealthMonitorQo healthMonitorQo) {

        SemRecord semRecord = new SemRecord();
        EhmResident ehmResident = ehmResidentService.findOne(Long.valueOf(healthMonitorQo.getDataId()));
        BasUser basUser = basUserService.findOne(ehmResident.getUid());

            //新增记录项
            for (HealthMonitorDeviceQo healthMonitorDeviceQo : healthMonitorQo.getDevices()) {
                for (HealthMonitorItemQo healthMonitorItemQo : healthMonitorDeviceQo.getItems()) {
                    Long id = 0l;
                    if (SemConst.DVTYPE_MAP.containsKey(healthMonitorItemQo.getItemSn())) {
                        semRecord.setDvtype(SemConst.DVTYPE_MAP.get(healthMonitorItemQo.getItemSn()));
                    } else {
                        continue;
                    }
                    switch (semRecord.getDvtype()) {
                        case SemConst.DBP:
                            //舒张压
                            SemBp semBp = new SemBp();
                            SemBpQo semBpQo = new SemBpQo();
                            semBpQo.setStatus(SemConst.STATUS);
                            semBpQo.setDevicesn(healthMonitorItemQo.getMeasureSn());
                            List<SemBp> semBps = semBpService.list(semBpQo);
                            if (semBps != null && semBps.size() > 0) {
                                semBp = semBps.get(0);
                                semBp.setDbp(Short.valueOf(healthMonitorItemQo.getMeasureValue()));
                                if (StringUtil.isNotBlank(healthMonitorItemQo.getLower()) && StringUtil.isNotBlank(healthMonitorItemQo.getUpper()))
                                    semBp.setDbpRrs(healthMonitorItemQo.getLower() + "-" + healthMonitorItemQo.getUpper());
                                if (SemConst.RESULT_MAP.containsKey(healthMonitorItemQo.getMeasureTip())) {
                                    semBp.setDbpResult(SemConst.RESULT_MAP.get(healthMonitorItemQo.getMeasureTip()));
                                } else {
                                    semBp.setDbpResult("-");
                                }
                                semBpService.update(semBp);
                                id = -1l;
                                break;
                            }
                            semBp.setStatus(SemConst.STATUS);
                            if (StringUtil.isNotBlank(healthMonitorDeviceQo.getDeviceTitle())) {
                                semBp.setDvsource(healthMonitorDeviceQo.getDeviceTitle());
                            } else {
                                semBp.setDvsource(SemConst.DV);
                            }
                            if (StringUtil.isNotBlank(healthMonitorItemQo.getMeasureUnits())) {
                                semBp.setUnits(healthMonitorItemQo.getMeasureUnits());
                            } else {
                                semBp.setUnits(SemConst.BPUNITS);
                            }
                            semBp.setResidentId(ehmResident.getId());
                            semBp.setUid(ehmResident.getUid());
                            semBp.setMeterdate(healthMonitorDeviceQo.getMeasureDate());
                            semBp.setDbp(Short.valueOf(healthMonitorItemQo.getMeasureValue()));
                            if (StringUtil.isNotBlank(healthMonitorItemQo.getLower()) && StringUtil.isNotBlank(healthMonitorItemQo.getUpper()))
                                semBp.setDbpRrs(healthMonitorItemQo.getLower() + "-" + healthMonitorItemQo.getUpper());
                            semBp.setDevicesn(healthMonitorItemQo.getMeasureSn());//监测平台的sn
                            if (SemConst.RESULT_MAP.containsKey(healthMonitorItemQo.getMeasureTip())) {
                                semBp.setDbpResult(SemConst.RESULT_MAP.get(healthMonitorItemQo.getMeasureTip()));
                            } else {
                                semBp.setDbpResult("-");
                            }
                            id = semBpService.save(semBp);
                            break;
                        case SemConst.HEIGHT:
                            //身高
                            BigDecimal bd = new BigDecimal(healthMonitorItemQo.getMeasureValue());
                            bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
                            SemHeight semHeight = new SemHeight();
                            semHeight.setDevicesn(healthMonitorItemQo.getMeasureSn());
                            semHeight.setStatus(SemConst.STATUS);
                            if (StringUtil.isNotBlank(healthMonitorDeviceQo.getDeviceTitle())) {
                                semHeight.setDvsource(healthMonitorDeviceQo.getDeviceTitle());
                            } else {
                                semHeight.setDvsource(SemConst.DV);
                            }
                            semHeight.setMeterdate(healthMonitorDeviceQo.getMeasureDate());
                            semHeight.setHeight(bd);
                            if (StringUtil.isNotBlank(healthMonitorItemQo.getMeasureUnits())) {
                                semHeight.setUnits(healthMonitorItemQo.getMeasureUnits());
                            } else {
                                semHeight.setUnits(SemConst.HEIGHTUNITS);
                            }
                            semHeight.setUid(ehmResident.getUid());
                            semHeight.setResidentId(ehmResident.getId());
                            if (StringUtil.isNotBlank(healthMonitorItemQo.getLower()) && StringUtil.isNotBlank(healthMonitorItemQo.getUpper()))
                                semHeight.setRrs(healthMonitorItemQo.getLower() + "-" + healthMonitorItemQo.getUpper());
                            id = semHeightService.save(semHeight);
                            break;
                        case SemConst.WEIGHT:
                            //体重
                            BigDecimal bd1 = new BigDecimal(healthMonitorItemQo.getMeasureValue());
                            bd1 = bd1.setScale(1, BigDecimal.ROUND_HALF_UP);
                            SemWeight semWeight = new SemWeight();
                            semWeight.setStatus(SemConst.STATUS);
                            if (StringUtil.isNotBlank(healthMonitorDeviceQo.getDeviceTitle())) {
                                semWeight.setDvsource(healthMonitorDeviceQo.getDeviceTitle());
                            } else {
                                semWeight.setDvsource(SemConst.DV);
                            }
                            semWeight.setUid(ehmResident.getUid());
                            semWeight.setResidentId(ehmResident.getId());
                            semWeight.setWeight(bd1);
                            semWeight.setDevicesn(healthMonitorItemQo.getMeasureSn());
                            if (StringUtil.isNotBlank(healthMonitorItemQo.getMeasureUnits())) {
                                semWeight.setUnits(healthMonitorItemQo.getMeasureUnits());
                            } else {
                                semWeight.setUnits(SemConst.WEIGHTUNITS);
                            }
                            semWeight.setMeterdate(healthMonitorDeviceQo.getMeasureDate());
                            id = semWeightService.save(semWeight);
                            break;
                        case SemConst.STEP:
                            //记步
                            SemStepQo semStepQo = new SemStepQo();
                            semStepQo.setStatus(SemConst.STATUS);
                            semStepQo.setDevicesn(healthMonitorItemQo.getMeasureSn());
                            List<SemStep> semSteps = semStepService.list(semStepQo);
                            if (semSteps != null && semSteps.size() > 0) {
                                SemStep semStep = semSteps.get(0);
                                semStep.setStepNum(Integer.valueOf(healthMonitorItemQo.getMeasureValue()));
                                semStep.setMeterdate(healthMonitorDeviceQo.getMeasureDate());
                                semStepService.update(semStep);
                                SemRecordQo semRecordQo = new SemRecordQo();
                                semRecordQo.setDataId(semStep.getId());
                                semRecordQo.setDvtype(SemConst.STEP);
                                List<SemRecord> records = service.find(semRecordQo);
                                SemRecord semRecord1 = records.get(0);
                                semRecord1.setMeterdate(healthMonitorDeviceQo.getMeasureDate());
                                service.update(semRecord1);
                                id = -1l;
                                break;
                            }else {
                                SemStep semStep = new SemStep();
                                semStep.setStatus(SemConst.STATUS);
                                semStep.setMeterdate(healthMonitorDeviceQo.getMeasureDate());
                                semStep.setResidentId(ehmResident.getId());
                                semStep.setCompanyId(ehmResident.getCompanyId());
                                semStep.setStepNum(Integer.valueOf(healthMonitorItemQo.getMeasureValue()));
                                semStep.setUid(ehmResident.getUid());
                                if (StringUtil.isNotBlank(healthMonitorDeviceQo.getDeviceTitle())) {
                                    semStep.setDvsource(healthMonitorDeviceQo.getDeviceTitle());
                                } else if (StringUtil.isNotBlank(healthMonitorDeviceQo.getDeviceSn()) && healthMonitorDeviceQo.getDeviceSn()!= "00"){
                                    semStep.setDvsource(healthMonitorDeviceQo.getDeviceSn());
                                } else {
                                    semStep.setDvsource(SemConst.DV);
                                }
                                if (StringUtil.isNotBlank(healthMonitorItemQo.getMeasureUnits())) {
                                    semStep.setUnits(healthMonitorItemQo.getMeasureUnits());
                                } else {
                                    semStep.setUnits(SemConst.STEPUNITS);
                                }
                                semStep.setDevicesn(healthMonitorItemQo.getMeasureSn());
                                List<SemStep> semSteps1 = semStepService.list(semStepQo);
                                for (SemStep step:semSteps1){
                                    SemRecordQo recordQo = new SemRecordQo();
                                    recordQo.setDvtype(SemConst.STEP);
                                    recordQo.setDataId(step.getId());
                                    service.delete(recordQo);
                                }
                                semStepServices.delete(semStepQo);
                                id = semStepService.save(semStep);
                                break;
                            }
                        case SemConst.SBP:
                            //收缩压
                            semRecord.setDvtype(SemConst.BP);
                            SemBp semBp1 = new SemBp();
                            SemBpQo semBpQo1 = new SemBpQo();
                            semBpQo1.setStatus(SemConst.STATUS);
                            semBpQo1.setDevicesn(healthMonitorItemQo.getMeasureSn());
                            List<SemBp> semBps1 = semBpService.list(semBpQo1);
                            if (semBps1 != null && semBps1.size() > 0) {
                                semBp1 = semBps1.get(0);
                                semBp1.setSbp(Short.valueOf(healthMonitorItemQo.getMeasureValue()));
                                if (StringUtil.isNotBlank(healthMonitorItemQo.getLower()) && StringUtil.isNotBlank(healthMonitorItemQo.getUpper()))
                                    semBp1.setSbpRrs(healthMonitorItemQo.getLower() + "-" + healthMonitorItemQo.getUpper());
                                if (SemConst.RESULT_MAP.containsKey(healthMonitorItemQo.getMeasureTip())) {
                                    semBp1.setSbpResult(SemConst.RESULT_MAP.get(healthMonitorItemQo.getMeasureTip()));
                                } else {
                                    semBp1.setSbpResult("-");
                                }
                                semBpService.update(semBp1);
                                id = -1l;
                                break;
                            }
                            semBp1.setStatus(SemConst.STATUS);
                            if (StringUtil.isNotBlank(healthMonitorDeviceQo.getDeviceTitle())) {
                                semBp1.setDvsource(healthMonitorDeviceQo.getDeviceTitle());
                            } else {
                                semBp1.setDvsource(SemConst.DV);
                            }
                            if (StringUtil.isNotBlank(healthMonitorItemQo.getMeasureUnits())) {
                                semBp1.setUnits(healthMonitorItemQo.getMeasureUnits());
                            } else {
                                semBp1.setUnits(SemConst.BPUNITS);
                            }
                            semBp1.setResidentId(ehmResident.getId());
                            semBp1.setUid(ehmResident.getUid());
                            semBp1.setMeterdate(healthMonitorDeviceQo.getMeasureDate());
                            semBp1.setSbp(Short.valueOf(healthMonitorItemQo.getMeasureValue()));
                            if (StringUtil.isNotBlank(healthMonitorItemQo.getLower()) && StringUtil.isNotBlank(healthMonitorItemQo.getUpper()))
                                semBp1.setSbpRrs(healthMonitorItemQo.getLower() + "-" + healthMonitorItemQo.getUpper());
                            semBp1.setDevicesn(healthMonitorItemQo.getMeasureSn());//监测平台的sn
                            if (SemConst.RESULT_MAP.containsKey(healthMonitorItemQo.getMeasureTip())) {
                                semBp1.setSbpResult(SemConst.RESULT_MAP.get(healthMonitorItemQo.getMeasureTip()));
                            } else {
                                semBp1.setSbpResult("-");
                            }
                            id = semBpService.save(semBp1);
                            break;
                        default:
                            break;
                    }
                    if (id > 0) {
                        //记录
                        semRecord.setStatus(SemConst.STATUS);
                        semRecord.setMeterdate(healthMonitorDeviceQo.getMeasureDate());
                        semRecord.setDataId(id);
                        semRecord.setResidentId(ehmResident.getId());
                        semRecord.setCompanyId(ehmResident.getCompanyId());
                        semRecord.setUid(ehmResident.getUid());
                        semRecord.setIdcard(basUser.getIdcard());
                        service.save(semRecord);
                    }


                }


            }


       return 0l;


    }
}